#!/bin/sh
# Copyright (c) 2005 Xilinx, Inc.  All rights reserved. 
# 
# Xilinx, Inc. 
# XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A 
# COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS 
# ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR 
# STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION 
# IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE 
# FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION. 
# XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO 
# THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO 
# ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE 
# FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY 
# AND FITNESS FOR A PARTICULAR PURPOSE. 
#

TP_PCUSB_HEX_FILE="xusbdfwu.hex"

TP_KERNEL_VERSION=`uname -r`
# check if udev is running on version 2.6.13* and later
TP_MAJOR_VERSION=`echo $TP_KERNEL_VERSION | cut -f 1 -d "."`
TP_MINOR_VERSION=`echo $TP_KERNEL_VERSION | cut -f 2 -d "."`
TP_MINOR_SUBVERSION=`echo $TP_KERNEL_VERSION | cut -f 3 -d "." | cut -f 1 -d "-"`

TP_USE_UDEV="0"
TP_UDEV_ENABLED=`ps -e | grep -c udevd`
if [ $TP_MAJOR_VERSION = "2" ]
then
	if [ $TP_MINOR_VERSION = "6" ]
	then
		test $TP_MINOR_SUBVERSION -ge "13"
		if [ $? = "0" ]
		then
			if [ $TP_UDEV_ENABLED = "1" ]
			then
				TP_USE_UDEV="1"
			fi;
		fi;
	fi;
fi;	

# $1 - destination directory
# $2 - driver name
update_hex_file ()
{
	TP_INSTALL_DRIVER="1"  # default to install
	#TP_DRV_SRC_PATH="/etc/hotplug/usb/xusbdfwu.fw"
	TP_DRIVER_FILE=$1/$2
	get_hex_file_version $TP_DRIVER_FILE
	if [ $? = "1" ]
	then
		TP_DEST_VERSION=$TP_DRIVER_VER
		TP_DRIVER_FILE=$2
		get_hex_file_version $TP_DRIVER_FILE
		TP_SRC_VERSION=$TP_DRIVER_VER
		if [ $TP_DEST_VERSION -lt $TP_SRC_VERSION ]
		then
			echo "--File $TP_DRIVER_FILE is newer than the destination file."
		else
			echo "--File $TP_DRIVER_FILE is already updated."
			TP_INSTALL_DRIVER="0"
		fi;
	else
		echo "--Error getting file version for $TP_DRIVER_FILE."
	fi;

	if [ $TP_INSTALL_DRIVER = "1" ]
	then
		echo "--Updating $2 file." 
		# Copy hex file to the fw directory.
		mkdir -p $1
		/bin/cp -p $2 $1 
	fi;
}

update_usermap ()
{
	TP_ID_PRESENT=`cat /etc/hotplug/usb.usermap | grep xusbdfwu | grep -i -c $1`
	if [ $TP_ID_PRESENT = "0" ]
	then
		echo "--Adding Product ID $1 to the usermap."
		echo "xusbdfwu             0x0003      0x03fd    0x$1   0x0000       0x0000       0x00         0x00            0x00        0x00            0x00               0x00               0x00000000" >> /etc/hotplug/usb.usermap
	else
		echo "--Product ID $1 is already in the usermap."
	fi;
}

file_exists () 
{
	if [ -f $1 ]
	then
		echo "--File $1 exists."
		return 1
	else
		echo "--File $1 does not exist."
		return 0
	fi;
}

get_hex_file_version ()
{
	TP_DRIVER_VER="0000"
	file_exists $1
	if [ $? = "1" ]
	then 
		TP_VERSION_STRING=`grep ":0219B900" $1`
		#TP_HEX_VERSION=`expr substr $TP_VERSION_STRING 10 4`
		TP_HEX_VERSION=${TP_VERSION_STRING:9:4}
		TP_DRIVER_VER=`printf "%d" 0x$TP_HEX_VERSION` 
		echo "--File $1 version = "$TP_DRIVER_VER
		return 1
	else
		return 0
	fi;
}

# arg1 = file, arg2 = version string
get_rules_file_version ()
{
	TP_DRIVER_VER="0000"
	file_exists $1
	if [ $? = "1" ]
	then 
		TP_VER_STR_LINE=`cat $1 | grep "$2"`
		TP_DRIVER_VER=${TP_VER_STR_LINE:10:4}
		echo "--File $1 version = "$TP_DRIVER_VER
		return 1
	else
		return 0
	fi;
}

if [ $TP_USE_UDEV = "0" ]
then
	update_hex_file /etc/hotplug/usb/xusbdfwu.fw xusbdfwu.hex
	update_hex_file /etc/hotplug/usb/xusbdfwu.fw xusb_xlp.hex
	update_hex_file /etc/hotplug/usb/xusbdfwu.fw xusb_emb.hex
	update_hex_file /etc/hotplug/usb/xusbdfwu.fw xusb_xpr.hex
	update_hex_file /etc/hotplug/usb/xusbdfwu.fw xusb_xup.hex
	update_hex_file /etc/hotplug/usb/xusbdfwu.fw xusb_xp2.hex
	update_hex_file /etc/hotplug/usb/xusbdfwu.fw xusb_xse.hex

	# Copy loader script file to the usb directory.
	/bin/cp -p xusbdfwu /etc/hotplug/usb 
	chmod 775 /etc/hotplug/usb/xusbdfwu
	/bin/cp -p xusb_xlp /etc/hotplug/usb 
	chmod 775 /etc/hotplug/usb/xusb_xlp
	/bin/cp -p xusb_emb /etc/hotplug/usb 
	chmod 775 /etc/hotplug/usb/xusb_emb
	/bin/cp -p xusb_xpr /etc/hotplug/usb 
	chmod 775 /etc/hotplug/usb/xusb_xpr
	/bin/cp -p xusb_xup /etc/hotplug/usb 
	chmod 775 /etc/hotplug/usb/xusb_xup
	/bin/cp -p xusb_xp2 /etc/hotplug/usb 
	chmod 775 /etc/hotplug/usb/xusb_xp2
	/bin/cp -p xusb_xse /etc/hotplug/usb 
	chmod 775 /etc/hotplug/usb/xusb_xse

	# Add entries to usermap
	update_usermap "0007"
	update_usermap "0009"
	update_usermap "000d"
	update_usermap "000f"
	update_usermap "0013"
	update_usermap "0015"

	update_usermap "0008"

else
	
	update_hex_file /usr/share xusbdfwu.hex
	update_hex_file /usr/share xusb_xlp.hex
	update_hex_file /usr/share xusb_emb.hex
	update_hex_file /usr/share xusb_xpr.hex
	update_hex_file /usr/share xusb_xup.hex
	update_hex_file /usr/share xusb_xp2.hex
	update_hex_file /usr/share xusb_xse.hex

	TP_INSTALL_DRIVER="1"
	TP_DRIVER_FILE="/etc/udev/rules.d/xusbdfwu.rules"		
	get_rules_file_version $TP_DRIVER_FILE "# version"		
	if [ $? = "1" ]
	then
		TP_DEST_VERSION=$TP_DRIVER_VER
		TP_DRIVER_FILE="xusbdfwu.rules"		
		get_rules_file_version $TP_DRIVER_FILE "# version"		
		TP_SRC_VERSION=$TP_DRIVER_VER
		if [ $TP_DEST_VERSION -lt $TP_SRC_VERSION ]
		then
			echo "--File $TP_DRIVER_FILE is newer than the destination file."
		else
			echo "--File $TP_DRIVER_FILE is already updated."
			TP_INSTALL_DRIVER="0"
		fi;
	else
		echo "--Error getting file version for $TP_DRIVER_FILE."
	fi;

	if [ $TP_INSTALL_DRIVER = "1" ]
	then
		echo "--Updating rules file." 
		/bin/cp -p xusbdfwu.rules /etc/udev/rules.d 
	fi;
fi;

# End of file.
